import os


def setup(max_rate, length, ips):
	clear_command_root = "tc qdisc del dev br0 root"
	os.call(clear_command_root)
	
	setup_command_root = "tc qdisc add dev br0 root handle 1:0 htb"
	os.call(setup_command_root)
	
	setup_command_mainclass = "tc class add dev br0 parent 1:1 classid 1:6 htb rate " + max_rate + " ceil " + max_rate
	os.call(setup_command_mainclass)
	
	for i in range(length):
		setup_command_personalclass_root = "tc class add dev br0 parent 1:1 classid 1:6" + i + " htb rate " + (max_rate/length) + "bps ceil " + max_rate + "bps default 6" + i + "2"
		os.call(setup_command_personalclass_root)
		
		setup_command_personalfilter_root_src = "tc filter add dev br0 parent 1:0 protocol ip prio 5 u32 match ip src 192.168.1." + i + "/32 flowid 1:6" + i
		setup_command_personalfilter_root_dst = "tc filter add dev br0 parent 1:0 protocol ip prio 5 u32 match ip dst 192.168.1." + i + "/32 flowid 1:6" + i
		
		os.call(setup_command_personalfilter_root_src)
		os.call(setup_command_personalfilter_root_dst)
		
		setup_command_personalclass_web = "tc class add dev br0 parent 1:6" + i + " classid 1:6" + i + "1 htb rate " + ((max_rate/length) * 0.8) + "bps ceil " + max_rate + "bps"
		setup_command_personalclass_other = "tc class add dev br0 parent 1:6" + i + " classid 1:6" + i + "2 htb rate " + ((max_rate/length) * 0.2) + "bps ceil " + max_rate + "bps"
		setup_command_personalfilter_web1 = "tc filter add dev br0 parent 1:6" + i + " protocol ip prio 5 u32 match ip dport 80 0xffff flowid 1:6" + i + "1"
		setup_command_personalfilter_web2 = "tc filter add dev br0 parent 1:6" + i + " protocol ip prio 5 u32 match ip sport 80 0xffff flowid 1:6" + i + "1"
		setup_command_personalfilter_web3 = "tc filter add dev br0 parent 1:6" + i + " protocol ip prio 5 u32 match ip dport 443 0xffff flowid 1:6" + i + "1"
		setup_command_personalfilter_web4 = "tc filter add dev br0 parent 1:6" + i + " protocol ip prio 5 u32 match ip sport 443 0xffff flowid 1:6" + i + "1"
		
		os.call(setup_command_personalclass_web)
		os.call(setup_command_personalclass_other)
		os.call(setup_command_personalfilter_web1)
		os.call(setup_command_personalfilter_web2)
		os.call(setup_command_personalfilter_web3)
		os.call(setup_command_personalfilter_web4)
		
		
def setup2(max_rate, address_data):
	
	ips = address_data.keys()
	length = len(ips)
	
	clear_command_root = "tc qdisc del dev br0 root"
	os.call(clear_command_root)
	
	setup_command_root = "tc qdisc add dev br0 root handle 1:0 htb"
	os.call(setup_command_root)
	
	setup_command_mainclass = "tc class add dev br0 parent 1:1 classid 1:6 htb rate " + max_rate + " ceil " + max_rate
	os.call(setup_command_mainclass)
	
	for i in ips:
		punish = address_data[i]
		
		setup_command_personalclass_root = "tc class add dev br0 parent 1:1 classid 1:6" + i + " htb rate " + (max_rate/length) + "bps ceil " + (max_rate/punish) + "bps default 6" + i + "2"
		os.call(setup_command_personalclass_root)
		
		setup_command_personalfilter_root_src = "tc filter add dev br0 parent 1:0 protocol ip prio 5 u32 match ip src 192.168.1." + i + "/32 flowid 1:6" + i
		setup_command_personalfilter_root_dst = "tc filter add dev br0 parent 1:0 protocol ip prio 5 u32 match ip dst 192.168.1." + i + "/32 flowid 1:6" + i
		os.call(setup_command_personalfilter_root_src)
		os.call(setup_command_personalfilter_root_dst)
		
		setup_command_personalclass_web = "tc class add dev br0 parent 1:6" + i + " classid 1:6" + i + "1 htb rate " + ((max_rate/length) * 0.8) + "bps ceil " + (max_rate/punish) + "bps"
		setup_command_personalclass_other = "tc class add dev br0 parent 1:6" + i + " classid 1:6" + i + "2 htb rate " + ((max_rate/length) * 0.2) + "bps ceil " + (max_rate/punish) + "bps"
		setup_command_personalfilter_web1 = "tc filter add dev br0 parent 1:6" + i + " protocol ip prio 5 u32 match ip dport 80 0xffff flowid 1:6" + i + "1"
		setup_command_personalfilter_web2 = "tc filter add dev br0 parent 1:6" + i + " protocol ip prio 5 u32 match ip sport 80 0xffff flowid 1:6" + i + "1"
		setup_command_personalfilter_web3 = "tc filter add dev br0 parent 1:6" + i + " protocol ip prio 5 u32 match ip dport 443 0xffff flowid 1:6" + i + "1"
		setup_command_personalfilter_web4 = "tc filter add dev br0 parent 1:6" + i + " protocol ip prio 5 u32 match ip sport 443 0xffff flowid 1:6" + i + "1"
		
		os.call(setup_command_personalclass_web)
		os.call(setup_command_personalclass_other)
		os.call(setup_command_personalfilter_web1)
		os.call(setup_command_personalfilter_web2)
		os.call(setup_command_personalfilter_web3)
		os.call(setup_command_personalfilter_web4)
